package com.link.speechdemo.utils;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

import com.link.speechdemo.MyApplication;
import com.link.speechdemo.bean.LocalMusic;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by link on 2016/11/10.
 * 音乐播放控制类
 */

public class PlayMusic {
    private MediaPlayer play;
    private Context context;
    private List<LocalMusic> musicList = new ArrayList<>();

    public PlayMusic() {
        context = MyApplication.getGlobalContext();
        musicList = MusicUtil.getMusic(context);
        play = new MediaPlayer();
    }
    public void startPlay(String artist, String song, String download) {
        //播放路径
        String path = null;
        for (LocalMusic musics : musicList) {
            Log.e("song", song);
            Log.e("name", musics.getName());
            if (song.equals(musics.getName())) {
                path = musics.getPath();
            } else {
                path = download;
            }
        }
        try {
            play.reset();//重置一下
            play.setDataSource(path);//绑定资源
            play.prepare();//准备播放
            play.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
